package com.breeze.leetcode.middle;

import java.util.Arrays;

/**
 * @url <a href="https://leetcode.cn/problems/design-memory-allocator/">2502. 设计内存分配器</a>
 * @author yangzh
 * @date 2025/2/25
 */
public class T2502Allocator {
    int[] memory;

    public static void main(String[] args) {
        T2502Allocator loc = new T2502Allocator(200);
        System.out.println("allocate: " + loc.allocate(146,235));
        System.out.println("allocate: " + loc.allocate(6,212));
        System.out.println("freeMemory: " + loc.freeMemory(235));
        System.out.println("allocate: " + loc.allocate(183,63));
        System.out.println("freeMemory: " + loc.freeMemory(274));
        System.out.println("allocate: " + loc.allocate(194,110));
        System.out.println("freeMemory: " + loc.freeMemory(38));
        System.out.println("freeMemory: " + loc.freeMemory(212));
        System.out.println("allocate: " + loc.allocate(240,49));
        System.out.println("freeMemory: " + loc.freeMemory(110));
        System.out.println("allocate: " + loc.allocate(278,118));
        System.out.println("freeMemory: " + loc.freeMemory(274));
        System.out.println("allocate: " + loc.allocate(200,298));
        System.out.println("allocate: " + loc.allocate(85,193));
        System.out.println("allocate: " + loc.allocate(69,118));
        System.out.println("freeMemory: " + loc.freeMemory(227));
        System.out.println("allocate: " + loc.allocate(35,33));
        System.out.println("allocate: " + loc.allocate(119,101));
        System.out.println("allocate: " + loc.allocate(270,51));
        System.out.println("allocate: " + loc.allocate(260,173));
        System.out.println("allocate: " + loc.allocate(11,80));
        System.out.println("freeMemory: " + loc.freeMemory(51));
        System.out.println("allocate: " + loc.allocate(33,65));
        System.out.println("allocate: " + loc.allocate(175,198));
        System.out.println("allocate: " + loc.allocate(163,260));
        System.out.println("allocate: " + loc.allocate(81,110));
        System.out.println("freeMemory: " + loc.freeMemory(204));
        System.out.println("allocate: " + loc.allocate(15,56));
        System.out.println("freeMemory: " + loc.freeMemory(260));
        System.out.println("allocate: " + loc.allocate(165,243));
        System.out.println("allocate: " + loc.allocate(65,193));
        System.out.println("allocate: " + loc.allocate(187,255));
        System.out.println("allocate: " + loc.allocate(250,227));
        System.out.println("freeMemory: " + loc.freeMemory(44));
        System.out.println("freeMemory: " + loc.freeMemory(243));
        System.out.println("allocate: " + loc.allocate(287,74));
        System.out.println("freeMemory: " + loc.freeMemory(38));
        System.out.println("freeMemory: " + loc.freeMemory(56));
        System.out.println("allocate: " + loc.allocate(177,121));
        System.out.println("allocate: " + loc.allocate(224,142));
        System.out.println("allocate: " + loc.allocate(241,284));
        System.out.println("allocate: " + loc.allocate(1,90));
        System.out.println("freeMemory: " + loc.freeMemory(221));
        System.out.println("freeMemory: " + loc.freeMemory(142));
        System.out.println("freeMemory: " + loc.freeMemory(65));
        System.out.println("allocate: " + loc.allocate(63,183));
        System.out.println("freeMemory: " + loc.freeMemory(148));
        System.out.println("allocate: " + loc.allocate(86,290));
        System.out.println("allocate: " + loc.allocate(109,188));
        System.out.println("freeMemory: " + loc.freeMemory(274));
        System.out.println("freeMemory: " + loc.freeMemory(221));
        System.out.println("freeMemory: " + loc.freeMemory(260));
        System.out.println("allocate: " + loc.allocate(89,179));
        System.out.println("allocate: " + loc.allocate(296,19));
        System.out.println("allocate: " + loc.allocate(265,293));
        System.out.println("allocate: " + loc.allocate(131,152));
        System.out.println("allocate: " + loc.allocate(85,77));
        System.out.println("freeMemory: " + loc.freeMemory(61));
        System.out.println("allocate: " + loc.allocate(259,283));
        System.out.println("allocate: " + loc.allocate(160,171));
        System.out.println("allocate: " + loc.allocate(151,230));
        System.out.println("freeMemory: " + loc.freeMemory(48));
        System.out.println("freeMemory: " + loc.freeMemory(42));
        System.out.println("freeMemory: " + loc.freeMemory(6));
        System.out.println("freeMemory: " + loc.freeMemory(254));
        System.out.println("freeMemory: " + loc.freeMemory(284));
        System.out.println("allocate: " + loc.allocate(230,224));
        System.out.println("allocate: " + loc.allocate(42,243));
        System.out.println("freeMemory: " + loc.freeMemory(155));
        System.out.println("allocate: " + loc.allocate(15,84));
        System.out.println("freeMemory: " + loc.freeMemory(125));
        System.out.println("freeMemory: " + loc.freeMemory(220));
        System.out.println("allocate: " + loc.allocate(24,300));
        System.out.println("freeMemory: " + loc.freeMemory(193));
        System.out.println("freeMemory: " + loc.freeMemory(51));
        System.out.println("allocate: " + loc.allocate(165,296));
        System.out.println("freeMemory: " + loc.freeMemory(77));
        System.out.println("freeMemory: " + loc.freeMemory(84));
        System.out.println("allocate: " + loc.allocate(152,231));
        System.out.println("freeMemory: " + loc.freeMemory(19));
        System.out.println("allocate: " + loc.allocate(249,64));
        System.out.println("allocate: " + loc.allocate(253,46));
        System.out.println("freeMemory: " + loc.freeMemory(42));
        System.out.println("freeMemory: " + loc.freeMemory(255));
        System.out.println("allocate: " + loc.allocate(47,242));
        System.out.println("allocate: " + loc.allocate(54,188));
        System.out.println("freeMemory: " + loc.freeMemory(192));
        System.out.println("allocate: " + loc.allocate(179,216));
        System.out.println("freeMemory: " + loc.freeMemory(128));
        System.out.println("freeMemory: " + loc.freeMemory(41));
        System.out.println("allocate: " + loc.allocate(123,276));
        System.out.println("allocate: " + loc.allocate(231,241));
        System.out.println("freeMemory: " + loc.freeMemory(298));
        System.out.println("freeMemory: " + loc.freeMemory(158));
        System.out.println("freeMemory: " + loc.freeMemory(177));
        System.out.println("allocate: " + loc.allocate(112,253));
        System.out.println("freeMemory: " + loc.freeMemory(173));
        System.out.println("freeMemory: " + loc.freeMemory(65));
        System.out.println("freeMemory: " + loc.freeMemory(38));
        System.out.println("freeMemory: " + loc.freeMemory(274));
        System.out.println("toString: " + Arrays.toString(loc.memory));
    }

    public T2502Allocator(int n) {
        memory = new int[n];
        Arrays.fill(memory, 0);
    }

    public int allocate(int size, int mID) {

        int count = 0;
        for (int i = 0; i < memory.length; i++) {
            if (memory[i] != 0) {
                count = 0;
            } else {
                count++;
                if (count == size) {
                    for (int j = i - count + 1; j <= i; j++) {
                        memory[j] = mID;
                    }
                    return i- count + 1;
                }
            }
        }

        return -1;
    }

    public int freeMemory(int mID) {
        int res = 0;
        for (int i = 0; i < memory.length; i++) {
            if (memory[i] == mID) {
                res++;
                memory[i] = 0;
            }
        }
        return res;
    }
}
